package com.barkerton.crawler.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Reads in and loads configuration properties used at start up
 * for boot strapping the application.
 * 
 * @author c.barker
 *
 */

public class PropertyManager {

	private static Log log = LogFactory.getLog(PropertyManager.class);
	
	private static final PropertyManager SINGLETON = new PropertyManager();
	private Properties props;
	
	/**
	 * Default constructor loads in and reads the 
	 */
	private PropertyManager() {
		props = new Properties();
		
		// TODO determine better route than hard coded absolute path
	    try {
	    	props.load(new FileInputStream("D:\\side_projects\\crawler\\Crawler\\crawler.properties"));
	    } catch (IOException ioe) {
	    	log.error("Unable to read properties file: " + ioe.getMessage());
		}
	}
	
	public static PropertyManager getInstance() {
		return SINGLETON;
	}
	
	/**
	 * Denotes whether or not property exists.	
	 * @param key	Key to check property for
	 * @return		Boolean
	 */
	public boolean hasProperty(String key) {
		return props.contains(key);
	}
	
	/**
	 * Looks up the key property value.  If none is found it
	 * will return null or the emptyValue suggested.
	 * 
	 * @param key			Property key to search for
	 * @param emptyValue	Value to return if none found
	 * @return				String of found property value
	 */
	public String getValue(String key, String emptyValue) {
		String value = props.getProperty(key, emptyValue).trim();

		if (value == null || value.trim().equals("") ) 
			value = emptyValue;
		
		return value;
	}
	
	public Properties getProperties() {
		return (Properties)props.clone();
	}
}
